package api;


import org.apache.zookeeper.AsyncCallback;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.concurrent.CountDownLatch;

public class ZKdelete {

    String ip = "azyjz.ltd:2181";
    private ZooKeeper zooKeeper;

    @Before
    public void before() throws Exception{
        final CountDownLatch countDownLatch = new CountDownLatch(1);
        zooKeeper= new ZooKeeper(ip, 50000, new Watcher() {
            public void process(WatchedEvent event) {
                System.out.println("创建连接成功");
                countDownLatch.countDown();
            }
        });
        countDownLatch.await();
    }

    @Test
    public void delete1() throws  Exception{
        // 同步删除节点
        // arg1:删除节点的节点路径
        // arg2:数据版本信息 -1代表删除节点时不考虑版本信息
        zooKeeper.delete("/create/node1",-1);


    }

    @Test
    public void delete2() throws  Exception{
        // 异步删除节点
        // 同步删除节点
        // arg1:删除节点的节点路径
        // arg2:数据版本信息 -1代表删除节点时不考虑版本信息
        // arg3:异步回调接口
        // arg4:上下文参数
       zooKeeper.delete("/create/node1", -1, new AsyncCallback.VoidCallback() {
           public void processResult(int rc, String path, Object ctx) {
               // 0代表删除成功
               System.out.println(rc);
                // 节点的路径
               System.out.println(path);
                // 上下文参数对象
               System.out.println(ctx);
           }
       },"im context");
        Thread.sleep(10000);
        System.out.println("结束");


    }

    @After
    public void after() throws InterruptedException {
        zooKeeper.close();
    }
}
